package top.abrsm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.commons.lang3.StringUtils;
import top.abrsm.entity.FileEntity;
import top.abrsm.entity.OrderEntity;
import top.abrsm.mapper.FileMapper;
import top.abrsm.mapper.OrderMapper;
import top.abrsm.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.abrsm.util.StateEnum;
import top.abrsm.util.StateException;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 贴吧订单表：置顶充值、积分充值 服务实现类
 * </p>
 * @author WuZH
 * @since 2023-11-18
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity> implements IOrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private FileMapper fileMapper;

    @Override
    public OrderEntity saveSingle (OrderEntity orderEntity) {
        if ( orderEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( orderEntity.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        orderEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( orderEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return orderEntity;
    }

    @Override
    public OrderEntity querySingle (OrderEntity orderEntity) {
        if ( orderEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        QueryWrapper<OrderEntity> queryWrapper = new QueryWrapper<> ();
        if ( StringUtils.isNotBlank ( orderEntity.getId () ) ) {
            queryWrapper.eq ( "id", orderEntity.getId () );
        }
        if ( StringUtils.isNotBlank ( orderEntity.getOutTradeNo () ) ) {
            queryWrapper.eq ( "outTradeNo", orderEntity.getOutTradeNo () );
        }
        OrderEntity order = baseMapper.selectOne ( queryWrapper );
        return order;
    }


    @Override
    public OrderEntity updateSingle (OrderEntity orderEntity) {
        if ( orderEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( orderEntity.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        UpdateWrapper<OrderEntity> updateWrapper = new UpdateWrapper<> ();
        if ( orderEntity.getTradeStatus () != null ) {
            updateWrapper.set ( "tradeStatus", orderEntity.getTradeStatus () );
        }
        updateWrapper.eq ( "id", orderEntity.getId () );
        orderEntity.setChangeTime ( new Date () );
        int update = baseMapper.update ( orderEntity, updateWrapper );
        if ( update == 0 ) {
            throw new StateException ( StateEnum.UPDATE_EXCE );
        }
        return orderEntity;
    }

    @Override
    public List<OrderEntity> queryList (OrderEntity orderEntity) {
        List<OrderEntity> orderEntities = orderMapper.queryList ( orderEntity.getStuId (), orderEntity.getTradeStatus (), orderEntity.getType (), ( orderEntity.getPageNum () - 1 ) * orderEntity.getPageSize (), orderEntity.getPageSize () );
        for ( OrderEntity entity : orderEntities ) {
            List<FileEntity> fileEntities = fileMapper.queryList ( entity.getProductId (), 2 );
            entity.setFileEntities ( fileEntities );
        }
        return orderEntities;
    }

    @Override
    public OrderEntity deleteSingle (OrderEntity orderEntity) {
        // 只有订单未付款和已完成才能删除订单
        orderEntity = baseMapper.selectById ( orderEntity );
        // 0状态表示订单未付款
        // 3状态表示订单已完成，已收到货物并不可退货退款
        if ( orderEntity.getTradeStatus () == 0 ) {
            int i = baseMapper.deleteById ( orderEntity );
            if ( i == 0 ) {
                throw new StateException ( StateEnum.DELETE_EXCE );
            }
        } else {
            throw new StateException ( StateEnum.DELETE_EXCE );
        }
        return orderEntity;
    }

}
